import 'dart:math';

import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:industry_net/base/common/util.dart';
import 'package:industry_net/base/component/dialog/bottom.dart';
import 'package:industry_net/base/component/dialog/drop.dart';
import 'package:industry_net/base/component/dragable.dart';
import 'package:industry_net/base/component/list/refresh.dart';
import 'package:industry_net/base/component/list/section.dart';
import 'models/home_menu.dart';
import 'models/home_news.dart';
import 'views/cell.dart';
import 'package:industry_net/pages/me/main/models/usually.dart';
import 'state.dart';

class HomeController extends GetxController {
  final state = HomeState();

  @override
  void onInit() {
    super.onInit();
    observeScroll();
    getSwiper();
    getMenu();
    getNews();
  }

  @override
  void onReady() {
    // dragableItem(
    //   md: UsuallyModel(name: '对话'),
    //   expandList: [
    //     UsuallyModel(name: '讲解'),
    //     UsuallyModel(name: '视频'),
    //     UsuallyModel(name: '提问'),
    //     UsuallyModel(name: '回答'),
    //     UsuallyModel(name: '问题'),
    //   ],
    // );
    super.onReady();
  }

  void observeScroll() {
    state.scrollController.addListener(() {
      var offY = state.scrollController.offset;
      if (offY <= SizeFit.navBarHeight) {
        var radio = max(offY / SizeFit.navBarHeight, 0);
        state.alpha.value = (255 * radio).toInt();
      } else if (state.alpha.value < 255) {
        state.alpha.value = 255;
      }
    });
  }

  void getSwiper() {
    state.swiperItems.value = List.generate(5, (index) => HomeSwiper());
  }

  void getNews() {
    var data = [
      {
        'avater':
            'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic4.zhimg.com%2F50%2Fv2-eb6852d04376f8abd86c4eb3e3746fa2_hd.jpg&refer=http%3A%2F%2Fpic4.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1638600102&t=35ceac500b13a2417c4d1ce9d631698d',
        'name': '医疗是否计入财政补贴项目',
        'look_count': '1,520',
        'time': '2021-11-04'
      },
      {
        'avater': 'https://t7.baidu.com/it/u=738441947,1208408731&fm=193&f=GIF',
        'name': '医疗是否计入财政补贴项目',
        'look_count': '1,520',
        'time': '2021-11-04'
      },
      {
        'avater':
            'https://pics2.baidu.com/feed/77094b36acaf2eddb724282fc585c6e03b0193b9.jpeg?token=f54d9d0e49f450d8be9a411c40581b54',
        'name': '医疗是否计入财政补贴项目',
        'look_count': '1,520',
        'time': '2021-11-04'
      },
      {
        'avater':
            'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic4.zhimg.com%2F50%2Fv2-eb6852d04376f8abd86c4eb3e3746fa2_hd.jpg&refer=http%3A%2F%2Fpic4.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1638600102&t=35ceac500b13a2417c4d1ce9d631698d',
        'name': '医疗是否计入财政补贴项目',
        'look_count': '1,520',
        'time': '2021-11-04'
      },
      {
        'avater': 'https://t7.baidu.com/it/u=738441947,1208408731&fm=193&f=GIF',
        'name': '医疗是否计入财政补贴项目',
        'look_count': '1,520',
        'time': '2021-11-04'
      },
      {
        'avater':
            'https://pics2.baidu.com/feed/77094b36acaf2eddb724282fc585c6e03b0193b9.jpeg?token=f54d9d0e49f450d8be9a411c40581b54',
        'name': '医疗是否计入财政补贴项目',
        'look_count': '1,520',
        'time': '2021-11-04'
      },
      {
        'avater':
            'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic4.zhimg.com%2F50%2Fv2-eb6852d04376f8abd86c4eb3e3746fa2_hd.jpg&refer=http%3A%2F%2Fpic4.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1638600102&t=35ceac500b13a2417c4d1ce9d631698d',
        'name': '医疗是否计入财政补贴项目',
        'look_count': '1,520',
        'time': '2021-11-04'
      },
      {
        'avater': 'https://t7.baidu.com/it/u=738441947,1208408731&fm=193&f=GIF',
        'name': '医疗是否计入财政补贴项目',
        'look_count': '1,520',
        'time': '2021-11-04'
      },
      {
        'avater':
            'https://pics2.baidu.com/feed/77094b36acaf2eddb724282fc585c6e03b0193b9.jpeg?token=f54d9d0e49f450d8be9a411c40581b54',
        'name': '医疗是否计入财政补贴项目',
        'look_count': '1,520',
        'time': '2021-11-04'
      },
    ];
    state.news = data
        .asMap()
        .map((key, value) =>
            MapEntry(key, HomeNewsModel.fromJson(value)..id = key.toString()))
        .values
        .toList();
    // state.news = List.generate(10,
    //     (index) => HomeNewsModel.fromJson(data.first)..id = index.toString());
    var sec = SectionModel(
        index: 0,
        itemBuilder: (context, index) => HomeNewsCell(mod: state.news[index]),
        items: state.news);
    state.newsSource.add(sec);
    update();
  }

  void getMenu() {
    // var data = [
    //   {
    //     'url':
    //         'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic4.zhimg.com%2F50%2Fv2-eb6852d04376f8abd86c4eb3e3746fa2_hd.jpg&refer=http%3A%2F%2Fpic4.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1638600102&t=35ceac500b13a2417c4d1ce9d631698d',
    //     'name': '医疗是否计入财政补贴项目',
    //     'look_count': '1,520',
    //     'time': '2021-11-04'
    //   }
    // ];

    state.menu = [
      HomeMenuModel()
        ..icon = 'menu_icon_Mall.png'
        ..name = '优选商城',
      HomeMenuModel()
        ..icon = 'menu_icon_financial.png'
        ..name = '政府补贴',
      HomeMenuModel()
        ..icon = 'menu_icon_pear.png'
        ..name = '现代梨园',
      HomeMenuModel()
        ..icon = 'menu_icon_field.png'
        ..name = '田头市场',
      HomeMenuModel()
        ..icon = 'menu_icon_loan.png'
        ..name = '贷款',
      HomeMenuModel()
        ..icon = 'menu_icon_supply.png'
        ..name = '供需大厅',
      HomeMenuModel()
        ..icon = 'menu_icon_coupons.png'
        ..name = '领券中心',
      HomeMenuModel()
        ..icon = 'menu_icon_more.png'
        ..name = '更多',
    ];
  }

  void toSearch() {
    // showCupertinoModalPopup(
    //     context: Get.context!,
    //     builder: (context) {
    //       return SearchPage(
    //         searchText: state.searchText,
    //       );
    //     });
    Get.toNamed(RouterPath.search);
  }

  void actionAdd() {
    showDropDown(
        key: state.addKey,
        list: [UsuallyModel(name: '扫一扫'), UsuallyModel(name: '添加好友')],
        onTap: (index) {
          Get.log(index.toString());
        });
  }

  void actionLocation() {
    // showAlert(msg: '内容信息');
    // showAlert(msg: '我是dialog内容', cancelText: null);
    // Get.toNamed(RouterPath.lottie_test);

    showActionSheet(titles: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']);
  }

  void actionApp(int index) {
    Get.toNamed(RouterPath.app_center);
  }

  void onRefresh() async {
    state.pageIndex = 1;
    await Future.delayed(const Duration(seconds: 3));
    RefreshHeaderFooter.stop(state.refreshCtrl, isSuccess: true);
  }

  void onLoading() async {
    state.pageIndex += 1;
    await Future.delayed(const Duration(seconds: 3));
    RefreshHeaderFooter.stop(state.refreshCtrl, isSuccess: true);
  }
}
